using Fusion.Frameworks.Version;
using UnityEditor;
using UnityEditor.Build.Content;
using UnityEngine;

namespace Fusion.Frameworks.Editor
{
    public enum BuildMode { 
        Debug,
        Release
    }

    public enum CompressType {
        LZMA = 0,
        Uncompressed = 1,
        LZ4 = 256,
    }

    public enum BuildTargetType {
        UseCurrentTarget = 0,
        Android = 13,
        iOS = 9,
        StandaloneWindows = 5,
        StandaloneWindows64 = 19,
        PS4 = 31,
        PS5 = 44,
        Switch = 38,
        WebGL = 20
    }


    [CreateAssetMenu(fileName = "Assets/GameAssets/BuildSetting", menuName = "FusionConfig/Build Setting")]
    public class BuildSetting : ScriptableObject
    {
        [SerializeField]
        public BuildMode buildMode = BuildMode.Release;
        [SerializeField]
        public CompressType compressType = CompressType.LZ4;

        [SerializeField]
        public BuildTargetType buildTargetType = BuildTargetType.UseCurrentTarget;

        [SerializeField]
        public SceneAsset initScene;

        [SerializeField]
        public VersionData version;

        private void OnValidate()
        {
            if (buildTargetType != BuildTargetType.UseCurrentTarget)
            {
                BuildTarget selectedBuildTarget = (BuildTarget)buildTargetType;
                if (selectedBuildTarget != EditorUserBuildSettings.activeBuildTarget)
                {
                    BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(selectedBuildTarget);

                    EditorUserBuildSettings.SwitchActiveBuildTarget(buildTargetGroup, selectedBuildTarget);
                }
            }
            

            if (buildMode == BuildMode.Release)
            {
                Utility.DeleteScriptingDefineSymbol("FUSION_DEBUG");
                Utility.AppendScriptingDefineSymbol("NO_PROFILER");
                Utility.AppendScriptingDefineSymbol("FUSION_RELEASE"); 
                Utility.DeleteScriptingDefineSymbol("DISABLE_ILRUNTIME_DEBUG"); 
            }
            else if (buildMode == BuildMode.Debug)
            {
                Utility.AppendScriptingDefineSymbol("FUSION_DEBUG");
                Utility.DeleteScriptingDefineSymbol("NO_PROFILER");
                Utility.DeleteScriptingDefineSymbol("FUSION_RELEASE");
                Utility.AppendScriptingDefineSymbol("DISABLE_ILRUNTIME_DEBUG");
            }
        }
    }
}
